package ink.ykb.mq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import ink.ykb.mq.config.DelayTime;
import ink.ykb.mq.config.WorkQueueRabbitConfig;
import ink.ykb.mq.config.RabbitTemplateUtil;
import ink.ykb.mq.dto.UserInfo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class MqController {

	@Autowired
	private RabbitTemplateUtil rabbitTemplateUtil;
	@Autowired
	private ObjectMapper objectMapper;

	@GetMapping("/mq/defult/publish")
	public String defultPublish(@RequestParam String msg) throws JsonProcessingException {
	  UserInfo info = new UserInfo();
	  info.setName("WERWER2323423");
	  info.setPassword(msg);
	  rabbitTemplateUtil.sendMsg(null, WorkQueueRabbitConfig.DEFULT_QUEUE,
	      objectMapper.writeValueAsString(info));
	  log.info("发送消息：{}", msg);
	  return msg;
	}
	
	@GetMapping("/mq/publish")
	public String publish(@RequestParam String msg) throws JsonProcessingException {
		UserInfo info = new UserInfo();
		info.setName("dfgdfgdf");
		info.setPassword(msg);
		rabbitTemplateUtil.sendMsg(WorkQueueRabbitConfig.TEST_EXCHANGE, WorkQueueRabbitConfig.TEST_QUEUE,
				objectMapper.writeValueAsString(info));
		log.info("发送消息：{}", msg);
		return msg;
	}

	@GetMapping("/mq/work/publish")
	public String workDirectPublish(@RequestParam String msg) {
		rabbitTemplateUtil.sendMsg(WorkQueueRabbitConfig.WORK_TEST_EXCHANGE, WorkQueueRabbitConfig.WORK_TEST_QUEUE,
				msg);
		log.info("发送消息：{}", msg);
		return msg;
	}

	@GetMapping("/mq/dead/publish")
	public String deadDirectPublish(@RequestParam String msg) {
		// rabbitTemplateUtil.sendDelayMsg(5000,msg);
		rabbitTemplateUtil.sendDelayMsg(WorkQueueRabbitConfig.WORK_TEST_EXCHANGE, WorkQueueRabbitConfig.WORK_TEST_QUEUE,
				DelayTime.SECOND_5, msg);
		log.info("发送消息：{}", msg);
		return msg;
	}

}
